/**
* @file				adc_config.c
* @author  		Piyumal Ranawaka st20000427
* @version 		0.1
* @date    		November 15, 2014
* @brief   		Contains the necessary functions to initialize pins and to use them for the Weather Monitoring System
*/



/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_adc.h"
#include "light_intensity_sensor.h"

/* Private types ------------------------------------------------------------------*/
/* Private constants --------------------------------------------------------------*/
#define GAIN=0.014652;    // Caliberation factor to map ADC register count with exact light intensity value lux GAIN=60/4095 . 


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

uint16_t adc_result; 			//final result of ADC conversion or register value
uint16_t lux_value; 	// final value of intensity in lux



/**
* @brief    Initialize Light Sensor
* @param    None
* @retval   None
*/

void light_intensity_config(void){

	RCC_Configuration_Adc3();	//sub method to configure RCC 
	GPIO_Configuration_Adc3();//sub method to configure GPIO
	ADC3_Configuration();			//sub method to configure ADC features
}



/**
* @brief    Configure RCC for lux sensor
* @param    None
* @retval   None
*/

void RCC_Configuration_Adc3(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
}




/**
* @brief    Configure GPIO for lux sensor
* @param    None
* @retval   None
*/
void GPIO_Configuration_Adc3(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_StructInit(&GPIO_InitStructure);

	//for ADC3 on Port A pin 2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
}


/**
* @brief    Configure ADC features for lux sensor
* @param    None
* @retval   None
*/

void ADC3_Configuration(void)
{
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	ADC_InitTypeDef       ADC_InitStructure;

	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
	ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
	ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

	ADC_CommonInit(&ADC_CommonInitStructure);

	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfConversion = 1;


	ADC_StructInit(&ADC_InitStructure);

	ADC_Init(ADC3, &ADC_InitStructure);

	ADC_RegularChannelConfig(ADC3, ADC_Channel_11, 1, ADC_SampleTime_480Cycles);
	ADC_EOCOnEachRegularChannelCmd(ADC3, ENABLE);

	ADC_Cmd(ADC3, ENABLE);    //The ADC is powered on by setting the ADON bit in the ADC_CR2 register.
	//When the ADON bit is set for the first time, it wakes up the ADC from the Power-down mode.

}



/**
* @brief    Method for reading light intensity in lux
* @param    None
* @retval   lux_value:final light intensity value in lux
*/
uint16_t light_intensity_read(void)
{
			ADC_SoftwareStartConv(ADC3);
			while(ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == RESET);
			adc_result= ADC_GetConversionValue(ADC);
			lux_value=adc_result*GAIN;	// convert register count into light intensity value.
			return lux_value;

}


